/**
 * 系统管理  公司管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>服务配置</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="formInline.deptName" placeholder="输入服务名称"></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="formInline.deptNo" placeholder="输入服务代码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable prop="id" label="服务ID" width="200">
      </el-table-column>
      <el-table-column sortable prop="servicename" label="服务名称" width="300">
      </el-table-column>
      <el-table-column sortable prop="money" label="价格" width="200">
      </el-table-column>
	  <el-table-column sortable prop="image" label="封面图" width="200">
		  <template slot-scope="scope">
		    <img v-if="scope.row.image" :src="scope.row.image" class="avatar" />
		  </template>
	  </el-table-column>
      <el-table-column align="center" sortable prop="isenable" label="是否启用" width="200">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isenable==1?true:false" @change="ewitchChange($event,scope.row.id)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column  label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form-item label="服务名称" prop="servicename">
          <el-input size="small" v-model="editForm.servicename" auto-complete="off" placeholder="请输入服务名称"></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="money">
          <el-input size="small" v-model="editForm.money" auto-complete="off" placeholder="请输入价格"></el-input>
        </el-form-item>
		<el-form-item label="封面图" prop="image">
		  <el-upload class="avatar-uploader" action="#2" :show-file-list="false" :http-request="upload" :before-upload="ImgBeforeAvatarUpload">
		  	<img v-if="editForm.image" :src="editForm.image" class="avatar" />
		  	<i v-else class="el-icon-plus avatar-uploader-icon"></i>
		  </el-upload>
		</el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getServiceList, addService, deleteService, modifyService, uploadImg } from '../../api/userMG'
import Pagination from '../../components/Pagination'
export default {
  data() {
    return {
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      editForm: {
        id: '',
        servicename: '',
        money: '',
		    image:''
      },
      // rules表单验证
      rules: {
        servicename: [
          { required: true, message: '请输入服务名称', trigger: 'blur' }
        ],
        money: [{ required: true, message: '请输入价格', trigger: 'blur' }],
		    image: [{ required: true, message: '请选择封面图', trigger: 'blur' }]
      },
      formInline: {
        // page: 1,
        // limit: 10,
      },
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  created() {
    this.getdata(this.formInline)
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
	  upload(f) {
	  	// console.log(f)
	  	// 1. 获取表单数据   fromData 表单数据
	  	let fromData = new FormData();
	  	fromData.append('file', f.file);
	  	// 2. 发起请求
	  	uploadImg(fromData)
	  		.then(res => {
	  			//3.请求成功之后  图片回显
	  			this.editForm.image = res.url;
	  		})
	  		.catch(err => {
	  			this.$message.error('上传图片失败');
	  		});
	  },
	  //上传前校验图片规则
	  ImgBeforeAvatarUpload(file) {
	  	// console.log(file);
	  	const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
	  	const isLt2M = file.size / 1024 / 1024 < 2;
	  	if (!isJPG) {
	  		this.$message.error('上传图片只能是 JPG 和 Png 格式!');
	  		return false;
	  	}
	  	if (!isLt2M) {
	  		this.$message.error('上传图片大小不能超过 2MB!');
	  		return false;
	  	}
	  },
    // 获取公司列表
    getdata(parameter) {
      // this.loading = true
      this.listData = [{id:1,servicename:'洗脚按摩',money:88,isenable:1,image:'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F4k%2Fs%2F02%2F2109242306111155-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1657795081&t=cb829e983dab7d02291136261fdb938c'}]
	  return;
      getServiceList(parameter)
        .then(res => {
          this.loading = false
           this.$message({
              type: 'info',
              message: res.message
            })
         this.listData = res.date;
            // 分页赋值
            // this.pageparm.currentPage = this.formInline.page
            // this.pageparm.pageSize = this.formInline.limit
            this.pageparm.total = res.count
        })
        .catch(err => {
          this.loading = false
          this.$message.error('菜单加载失败，请稍后再试！')
        })
    },
    // switch改变事件
    ewitchChange(e,id){
      let parameter={configID:id,isenable: e?'1':'0'}
      modifyService(parameter)
        .then(res => {
          this.getdata(this.formInline)
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline)
    },
    //显示编辑界面
    handleEdit: function(index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改'
        this.editForm.id = row.id
        this.editForm.servicename = row.servicename
        this.editForm.money = row.money
		    this.editForm.image = row.image
      } else {
        this.title = '添加'
        this.editForm.id = ''
        this.editForm.servicename = ''
        this.editForm.money = ''
		    this.editForm.image = ''
      }
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      console.log(editData)
      this.$refs[editData].validate(valid => {
        if (valid) {
          if( this.title = '添加' ){
              addService(this.editForm)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              this.getdata(this.formInline)
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
            })
          }else{
            modifyService(this.editForm)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              this.getdata(this.formInline)
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
            })
          }
          
        } else {
          return false
        }
      })
    },
    // 删除公司
    deleteUser(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          let parameter={configID:row.id};
          deleteService(parameter)
            .then(res => {
                this.getdata(this.formInline)
            })
            .catch(err => {
              this.loading = false
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false
    }
  }
}
</script>

<style scoped lang="less">
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
.avatar {
	width: 80px;
	height: 80px;
	display: block;
}
.avatar-uploader {
	.el-upload {
		border: 1px dashed #8c939d;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}
	.el-upload:hover {
		border-color: #409eff;
	}
}
.avatar-uploader-icon {
	border: 1px dashed #8c939d;
	font-size: 28px;
	color: #8c939d;
	width: 80px;
	height: 80px;
	line-height: 80px;
	text-align: center;
}
</style>

 